﻿using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPFStudy.PrismStudy.Event;

namespace WPFStudy.PrismStudy.ViewModels
{
    /// <summary>
    /// 发布订阅事件
    /// </summary>
    public class PubSubEventViewModel:BindableBase
    {
        private int _publishCount = 0;

        private readonly IEventAggregator _eventAggregator;

        #region 事件接收的消息

        /// <summary>
        /// 事件接收的消息
        /// </summary>
        private string _subscribeMessage;

        /// <summary>
        /// 事件接收的消息
        /// </summary>
        public string SubscribeMessage
        {
            get { return _subscribeMessage; }
            set { SetProperty(ref _subscribeMessage, value); }
        }

        #endregion 事件接收的消息 

        #region 发布消息

        /// <summary>
        /// 发布消息 - 命令
        /// </summary>
        private DelegateCommand _publishMessageCmd;

        /// <summary>
        /// 发布消息 - 命令
        /// </summary>
        public DelegateCommand PublishMessageCmd =>
            _publishMessageCmd ?? (_publishMessageCmd = new DelegateCommand(ExecutePublishMessage));

        /// <summary>
        /// 执行 - 发布消息
        /// </summary>    
        void ExecutePublishMessage()
        {
            //向MessageEvent发布一个了消息："发布了订阅"
            _eventAggregator.GetEvent<MessageEvent>().Publish($"发布了订阅 - {_publishCount++}");
        }

        #endregion 发布消息


        #region 订阅

        /// <summary>
        /// 订阅 - 命令
        /// </summary>
        private DelegateCommand _subscribeCmd;

        /// <summary>
        /// 订阅 - 命令
        /// </summary>
        public DelegateCommand SubscribeCmd =>
            _subscribeCmd ?? (_subscribeCmd = new DelegateCommand(ExecuteSubscribe));

        /// <summary>
        /// 执行 - 订阅
        /// </summary>    
        void ExecuteSubscribe()
        {
            //进行订阅
            _eventAggregator.GetEvent<MessageEvent>().Subscribe(HandleMessage);
            SubscribeStatus = "已订阅";
        }

        #endregion 订阅


        #region 订阅状态

        /// <summary>
        /// 订阅状态
        /// </summary>
        private string _subscribeStatus = "未订阅";

        /// <summary>
        /// 订阅状态
        /// </summary>
        public string SubscribeStatus
        {
            get { return _subscribeStatus; }
            set { SetProperty(ref _subscribeStatus, value); }
        }

        #endregion 订阅状态 

        #region 取消订阅

        /// <summary>
        /// 取消订阅 - 命令
        /// </summary>
        private DelegateCommand _unSubscribeCmd;

        /// <summary>
        /// 取消订阅 - 命令
        /// </summary>
        public DelegateCommand UnSubscribeCmd =>
            _unSubscribeCmd ?? (_unSubscribeCmd = new DelegateCommand(ExecuteUnSubscribe));

        /// <summary>
        /// 执行 - 取消订阅
        /// </summary>    
        void ExecuteUnSubscribe()
        {
            //取消订阅
            _eventAggregator.GetEvent<MessageEvent>().Unsubscribe(HandleMessage);
            SubscribeStatus = "已取消订阅";
        }

        #endregion 取消订阅

        /// <summary>
        /// 处理消息
        /// </summary>
        /// <param name="msg"></param>
        public void HandleMessage(string msg)
        {
            SubscribeMessage = msg;
        }

        public PubSubEventViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
        }
    }
}
